package com.yuanfeng.action.front.order.dao;

import com.yuanfeng.pojo.OrderLine;
import com.yuanfeng.pojo.Orders;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * 订单dao实现类
 * @author yuanfeng
 */
public class OrderDaoImpl extends HibernateDaoSupport implements OrderDao {
    /**
     * 保存订单
     * @param order
     */
    public void saveOrder(Orders order) {
        //保存到订单表
        Orders orders = new Orders();
        orders.setId(order.getId());
        orders.setUserid(order.getUserid());
        orders.setConsignee(order.getConsignee());
        orders.setPaytype(order.getPaytype());
        orders.setAmt(order.getAmt());
        orders.setState("0");
        orders.setOrderdate(order.getOrderdate());
        System.out.println("保存了orders");
        this.getHibernateTemplate().getSessionFactory().getCurrentSession().save(orders);
        //保存到订单详细列表
        List<OrderLine> lines = order.getOrderLines();
        OrderLine orderLine = new OrderLine();

        for (OrderLine ol : lines) {
            orderLine.setId(ol.getId());
            orderLine.setOrderid(ol.getOrderid());
            orderLine.setBookid(ol.getBookid());
            orderLine.setAmt(ol.getAmt());
            orderLine.setPrice(ol.getPrice());
            this.getHibernateTemplate().getSessionFactory().getCurrentSession().save(orderLine);
            this.getHibernateTemplate().getSessionFactory().getCurrentSession().flush();
            System.out.println("保存了orderLine");
        }
    }

    /**
     * 根据用户id查询订单
     *
     * @param userId
     * @return
     */
    public List<Orders> queryOrders(final String userId) {

        return this.getHibernateTemplate().executeFind(new HibernateCallback<List<Orders>>() {
            @Override
            public List<Orders> doInHibernate(Session session) throws HibernateException {
                String hql = "from Orders where userid=? order by orderdate desc";
                Query query = session.createQuery(hql);
                query.setParameter(0, userId);
                return query.list();
            }

        });
    }

    /**
     * 查询一个订单
     *
     * @param order
     * @return
     */
    public Orders oneOrder(final Orders order) {

        final List<Orders> order2 = this.getHibernateTemplate().executeFind(new HibernateCallback<List<Orders>>() {
            @Override
            public List<Orders> doInHibernate(Session session) throws HibernateException {
                String hql = "from Orders where id=?";
                Query query = session.createQuery(hql);
                query.setParameter(0, order.getId());
                return query.list();
            }
        });

        if (order2.get(0) != null) {
            List<OrderLine> lines = this.getHibernateTemplate().executeFind(new HibernateCallback<List<OrderLine>>() {
                @Override
                public List<OrderLine> doInHibernate(Session session) throws HibernateException {
                    String sql = "select ol.id as id,ol.orderid as orderid,ol.bookid as bookid,ol.amt as amt,ol.price as price," +
                            "round(ol.amt*ol.price,2) as sum," +
                            "b.name as name" +
                            " from OrderLine ol inner join Books b on ol.bookid=b.id" +
                            " where ol.orderid=?";
                    SQLQuery query = session.createSQLQuery(sql).addEntity(OrderLine.class);
                    query.setParameter(0, order2.get(0).getId());
                    List<OrderLine> orderLines = query.list();
                    return orderLines;
                }
            });
            order2.get(0).setOrderLines(lines);
            return order2.get(0);
        }
        return null;
    }

    /**
     * 取消订单
     * @param order
     */
    public void cancle(Orders order) {

        /**
         * 直接删除没有效果可能是由于没有flush
         */
        Orders orders = this.getHibernateTemplate().get(Orders.class, order.getId());
        this.getHibernateTemplate().delete(orders);
        this.getHibernateTemplate().flush();
        this.getHibernateTemplate().clear();

    }


}
